package com.tsing.bms.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsing.bms.dto.NameAndPhoneDto;
import com.tsing.bms.entity.House;
import com.tsing.bms.vo.req.HousePageReqVo;
import com.tsing.bms.vo.resp.HouseRageRespVo;
import com.tsing.bms.vo.resp.IdAndNameRespVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
public interface HouseMapper extends BaseMapper<House> {

    @Select("<script>" +
            "select h.*,u.name unitName,b.name buildingName,us.nick_name userName,us.phone userPhone\n" +
            "from t_house h\n" +
            "left join t_unit u on h.unit_id=u.id\n" +
            "left join t_building b on u.building_id=b.id\n" +
            "left join t_user us on h.id=us.house_id\n" +
            "<where>\n" +
            "<if test=\"reqVo.username!=null and reqVo.username!=''\">and us.nick_name like concat('%',#{reqVo.username},'%')</if>\n" +
            "<if test=\"reqVo.name!=null and reqVo.name!=''\">and h.name like concat('%',#{reqVo.name},'%')</if>\n" +
            "<if test=\"reqVo.householdId!=null and reqVo.householdId!=''\">and h.household_id = #{reqVo.householdId}</if>\n" +
            "</where>\n" +
            "</script>")
    List<HouseRageRespVo> housePage(HousePageReqVo reqVo, Page<House> page);

    @Select("<script>" +
            "select 1 from t_house h\n" +
            "inner join t_user u on h.id=u.house_id\n" +
            "where h.id in\n" +
            "<foreach collection='ids' item='item' open='(' close=')' separator=',' >\n" +
            "#{item}\n" +
            "</foreach>\n" +
            "limit 1" +
            "</script>")
    String haveRelevance(@Param("ids") List<String> ids);

    @Select("select id\n" +
            "from t_user\n" +
            "where house_id=#{id} limit 1")
    String selectUserid(@Param("id") String id);

    @Select("select nick_name name,phone from t_user where id=#{id}")
    NameAndPhoneDto selectZhuhu(@Param("id") String id);

    @Select("<script>" +
            "select id,nick_name name\n" +
            "from t_user\n" +
            "<if test=\" name!=null and name !='' \"> where nick_name like concat('%',#{name},'%') </if>" +
            "</script>")
    List<IdAndNameRespVo> userList(@Param("name") String name);

}
